<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
$(function(){
	$('#Users_Data').datagrid({
		fit: true,
		nowrap: false,
		striped: true,
		singleSelect: true,
		url: '<c:url value="/users/findUsersPage.do"/>',
		idField: 'id',
		frozenColumns:[[
           {field:'ck',checkbox:true}
   		]],
		columns: [[
            {field:'code',title:'家长编号',width:150},
			{field:'name',title:'家长姓名',width:120},
			{field:'relation',title:'与孩子关系',width:100},
			{field:'mobile',title:'手机号码 ',width:100},
			{field:'email',title:'邮箱',width:100},
			{field:'birthday',title:'生日',width:80},
			{field:'saleUser',title:'销售人员',width:110, formatter:function(value, rowData){
				if(rowData.saleUser == null) return;
				return rowData.saleUser.name;
			}},
			{field:'managerUser',title:'市场部经理',width:110, formatter:function(value, rowData){
				if(rowData.managerUser == null) return;
				return rowData.managerUser.name;
			}},
			{field:'assistantUser',title:'上网教育辅导助理',width:110, formatter:function(value, rowData){
				if(rowData.assistantUser == null) return;
				return rowData.assistantUser.name;
			}},
			{field:'addTime',title:'添加时间',width:110},
			{field:'lastTime',title:'最后更新时间',width:110}
		]],
		toolbar:[{
			text:'新增',
			iconCls:'icon-add',
			handler:function(){
				addUsers(1);
			}
		},{
			text:'删除',
			iconCls:'icon-remove',
			handler:delUsers
		},{
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				addUsers(2);
			}
		}],
		pageSize: 20,
		pagination: true,
		rownumbers: true
	});
});

//删除会员
function delUsers(){
	$.messager.alert('错误', '会员暂时无删除功能', 'error'); 
	return;
	var row = $('#Users_Data').datagrid('getSelected');
	if(row == null){
		$.messager.alert('错误', '请选择您要删除的数据！', 'error'); 
		return;
	}
	else {
		$.messager.confirm('警告', '您确认删除么?', function(r){
			if(r){
				$.ajax({
					url: '<c:url value="/users/"/>',
					type: 'POST',
					data: 'uuid='+row.uuid,
					beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
						$.messager.progress({
							title:'提示',
							msg:'正在执行数据操作，请稍待...'
						});
					},
					success: function(result) {
						$.messager.progress('close');
						if(result){
							$('#Users_Data').datagrid('load');
						}
						else{
							$.messager.alert('错误', '模块删除出错!', 'error');
						}
					}
				});
			}
		});
	}
}

//添加、修改会员
function addUsers(tag){
	var url = '<c:url value="/users/loadAddUsersPage.do"/>';
	var title = '';
	if(tag == 1){ //新增
		$('#Users_Data').datagrid('clearSelections');
		title = '新增';
	}
	else{ //编辑
		var row = $('#Users_Data').datagrid('getSelected');
		if(!row){
			$.messager.alert('错误', '请选中所需修改的数据!', 'error');
		}
		url += "?id=" + row.id;
		title = '修改';
	}
	
	$('#Users_').dialog('close').remove();
	$('body').append('<div id="Users_" class="easyui-dialog"></div>');
	$('#Users_').dialog({
		title:title,
		modal:true,
		closable:true,
		width:600,
		height:400,
		toolbar:[{
			text:'保存',
			iconCls:'icon-add',
			handler:saveUsers
		},{
			text:'取消',
			iconCls:'icon-remove',
			handler:function(){
				$('#Users_').dialog('close').remove();
			}
		}]
	}).dialog('open').dialog('refresh', url);
}

//保存会员 
function saveUsers(){
	if ($('#UsersForm').form('validate')){
		var data = $('#UsersForm').serialize();
		$.ajax({
			url: '<c:url value="/users/addUsers.do"/>',
			type: 'POST',
			data: data,
			beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
				$.messager.progress({
					title:'提示',
					msg:'正在执行数据操作，请稍待...'
				});
			},
			success: function(result) {
				$.messager.progress('close');
				if(result == 'success'){
					$('#Users_').dialog('close').remove();
					$('#Users_Data').datagrid('load');
				}
				else{
					$.messager.alert('错误', '会员添加出错!', 'error');
				}
			}
		});
	}
}
</script>
<table id="Users_Data"></table>
